<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        #box {
            width: 400px;
            height: 501px;
            border: 1px solid orangered;
            margin: 100px auto;
            font-size: 48px;
            text-align: center;
        }
        
        #ym {
            height: 100px;
            background-color: red;
            line-height: 100px;
        }
        
        #d {
            background-color: orange;
            height: 200px;
            line-height: 200px;
        }
        
        #w {
            height: 100px;
            line-height: 100px;
            background-color: rgb(0, 26, 255);
        }
        
        #hms {
            height: 100px;
            line-height: 100px;
            background-color: rgb(57, 146, 72);
        }
    </style>
</head>

<body>

    <div id="box">
        <div id='ym'></div>
        <div id='d'></div>
        <div id='w'></div>
        <div id='hms'></div>
    </div>
    <script>
        getDateAndTime();
        //启动定时器  调用函数getDateAndTime()
        window.setInterval(getDateAndTime, 1000)

        function getDateAndTime() {


            //获取系统当前的日期
            var date = new Date();
            //提取时间和日期

            var month = date.getMonth() + 1;
            var year = date.getFullYear();
            var day = date.getDate();
            var week = date.getDay();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            //处理week格式
            switch (week) {

                case 1:
                    week = '星期一';
                    break;
                case 2:
                    week = '星期二';
                    break;
                case 3:
                    week = '星期三';
                    break;

                case 4:
                    week = '星期四';
                    break;
                case 5:
                    week = '星期五';
                    break;
                case 6:
                    week = '星期六';
                    break;
                case 0:
                    week = '星期日';
                    break;

            }


            //处理时分秒
            function formatHMS(time) {
                if (time < 10) {
                    return '0' + time;
                } else {
                    return time;
                }

            }

            //获取页面元素
            var ym = document.getElementById('ym');
            var d = document.getElementById('d');
            var w = document.getElementById('w');
            var hms = document.getElementById('hms');

            //将时间写入到页面
            ym.innerHTML = `${year}年${month}月`;
            d.innerHTML = day + '日';
            w.innerHTML = week;
            hms.innerHTML = `${hour}时${minute}分${second}秒`;
        }
    </script>
</body>

</html>